package cn.redragon.soa.controller;

import com.digibridge.common.core.brand.Brand;
import com.digibridge.common.core.dto.PageResultDTO;
import com.digibridge.common.core.web.ResponseDTO;
import com.digibridge.loyalty.stock.amqp.model.StockImportedEvent;
import com.digibridge.loyalty.stock.data.criteria.StockHistoryCriteria;
import com.digibridge.loyalty.stock.dto.ProductDTO;
import com.digibridge.loyalty.stock.dto.StockDTO;
import com.digibridge.loyalty.stock.dto.StockHistoryDTO;
import com.digibridge.loyalty.stock.dto.StockReservedDTO;
import com.digibridge.loyalty.stock.enums.StockActionType;
import com.digibridge.loyalty.stock.enums.StockImportMode;
import com.digibridge.loyalty.stock.service.StockHistoryService;
import com.digibridge.loyalty.stock.service.StockService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/v1/{brand}/stock")
@Tag(name = "Stock API")
@Slf4j
@RequiredArgsConstructor
@Validated
public class StockController {

    private final StockService stockService;

    private final StockHistoryService stockHistoryService;

    @PostMapping
    @Operation(summary= "Get available stocks")
    @PreAuthorize("hasAuthority(#brand + ':STOCK:RW')")
    public ResponseDTO<List<StockDTO>> getStocks(@PathVariable Brand brand, @RequestBody @Valid List<ProductDTO> productDTOs) {
        return ResponseDTO.success(stockService.getStocks(brand.name(), productDTOs));
    }

    @GetMapping("/{type}/total")
    @Operation(summary= "Get total stocks for each type")
    @PreAuthorize("hasAuthority(#brand + ':STOCK:RW')")
    public ResponseDTO<Map<String, String>> getTotalStocks(@PathVariable Brand brand, @PathVariable String type) {
        return ResponseDTO.success(stockService.getTotalStocks(brand.name(), type));
    }

    @PutMapping
    @Operation(summary= "Import stocks with movements (+/-)")
    @PreAuthorize("hasAuthority(#brand + ':STOCK:RW')")
    public ResponseDTO<Void> importStocks(@PathVariable Brand brand, @RequestBody @Valid StockImportedEvent event, @RequestParam String profile, @RequestParam(defaultValue = "FULL")
    StockImportMode mode) {
        stockService.importStocks(brand.name(), event, StockActionType.IMPORT, profile, mode);
        return ResponseDTO.success();
    }

    @PostMapping("/check")
    @Operation(summary= "Check stocks are sufficient")
    @PreAuthorize("hasAuthority(#brand + ':STOCK:RW')")
    public ResponseDTO<Void> checkStocks(@PathVariable Brand brand, @RequestBody @Valid List<StockReservedDTO> stocks) {
        stockService.checkStocks(brand.name(), stocks);
        return ResponseDTO.success();
    }

    @GetMapping("/history")
    @Operation(summary= "Get stock histories")
    @PreAuthorize("hasAuthority(#brand + ':STOCK:RW')")
    public ResponseDTO<PageResultDTO<StockHistoryDTO>> getStockHistories(@PathVariable Brand brand, StockHistoryCriteria criteria) {
        criteria.setBrand(brand.name());
        return ResponseDTO.success(stockHistoryService.list(criteria));
    }

    @PostMapping("/history")
    @Operation(summary= "Create stock history")
    @PreAuthorize("hasAuthority(#brand + ':STOCK:RW')")
    public ResponseDTO<Void> createStockHistories(@PathVariable Brand brand, @RequestBody List<StockHistoryDTO> historyDTOs) {
        stockHistoryService.save(brand.name(), historyDTOs);
        return ResponseDTO.success();
    }
}
